package com.ruoyi.project.xkx.order.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.xkx.order.domain.ClassStudent;
import com.ruoyi.project.xkx.order.domain.YsOrder;
import com.ruoyi.project.xkx.school.domain.XkxClass;
import com.ruoyi.project.xkx.school.domain.XkxClassStudent;
import com.ruoyi.project.xkx.school.service.IXkxClassService;
import com.ruoyi.project.xkx.school.service.IXkxClassStudentService;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.xkx.order.mapper.XkOrderMapper;
import com.ruoyi.project.xkx.order.domain.XkOrder;
import com.ruoyi.project.xkx.order.service.IXkOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 消课订单Service业务层处理
 * 
 * @author 闫阳光
 * @date 2021-05-13
 */
@Service
public class XkOrderServiceImpl extends ServiceImpl<XkOrderMapper,XkOrder> implements IXkOrderService
{
    @Autowired
    private XkOrderMapper xkOrderMapper;
    @Autowired
    private IXkxClassStudentService iXkxClassStudentService;
    @Autowired
    private IXkxClassService iXkxClassService;
    /**
     * 查询消课订单
     *
     * @param xkId 消课订单ID
     * @return 消课订单
     */
    @Override
    public XkOrder selectXkOrderById(Long xkId)
    {
        return xkOrderMapper.selectXkOrderById(xkId);
    }

    /**
     * 查询消课订单列表
     *
     * @param xkOrder 消课订单
     * @return 消课订单
     */
    @Override
    public List<XkOrder> selectXkOrderList(XkOrder xkOrder)
    {
        return xkOrderMapper.selectXkOrderList(xkOrder);
    }

    /**
     * 新增消课订单
     * 
     * @param xkOrder 消课订单
     * @return 结果
     */
    @Override
    public int insertXkOrder(XkOrder xkOrder)
    {
        /**
         * 1 拿到 classId 查询班级下所有学生和该学生的课时剩余
         * 作用：
         * 1 计算学生扣除课时 已有课时-班级要求扣除课时
         * 2 计算消课订单金额 班级学生消耗课时的总数量 * （课程总价/该课程总课时）* 学生数量 = 班级的消课金额
         */
        Integer classId = Math.toIntExact(xkOrder.getClassId());
        //学生已有课时
        List<XkxClassStudent> classStudentHours = xkOrderMapper.getClassStudentHours(classId);
        //班级要求扣除课时(订单里来的)
        //循环对每个学生进行课时扣除
        for(int i=0;i<classStudentHours.size();i++){
            XkxClassStudent stuObj = classStudentHours.get(i);
            //当前学生剩余课时
            Integer studentHours = stuObj.getCourseHour();
            
            //班级要求扣除的课时
            Integer classDeductHours = stuObj.getXkxClass().getClassDeductHours();
            //当前学生计算后的剩余课时
            Integer finalHours = studentHours - classDeductHours;
            //新建维护学生课时对象进行更新
            XkxClassStudent objClaStu = new XkxClassStudent();
            //设置维护学生课时表 id
            objClaStu.setClassStudentId(stuObj.getClassStudentId());
            objClaStu.setClassId(stuObj.getClassId());
            objClaStu.setStudentId(stuObj.getStudentId());
            //设置剩余课时
            objClaStu.setCourseHour(finalHours);
            //学生状态
            objClaStu.setStudentCode(0);
            objClaStu.setAttendance("已签到");
            //更新对象ok
            iXkxClassStudentService.updateById(objClaStu);
        }
        //计算本班学生消耗金额 = 班级学生消耗课时的总数量 * （课程总价/课程课时数量）* 学生数量 = 班级的消课金额
        //班级学生数量
        Integer studentNumber = classStudentHours.size();
        //得到班级的目的 就是要知道这个班 授什么课，课程价格 默认扣除课时
        XkxClass clazz = xkOrderMapper.getClassDeCourseAndPrice(classId);
        //默认扣除课时
        Integer deductHours = clazz.getClassDeductHours();
        //学生使用的总课时数
        int totalHours = studentNumber*deductHours;
        //课程价格
        Double price = clazz.getCoursePrice();
        //该课程有多少课时
        Integer number = clazz.getCourseHour();
        //课程单价
        double hoursPrice = price/number;
        double totalPrice = totalHours*hoursPrice;
        XkOrder xkOrder1 = new XkOrder();
        xkOrder1.setXkDate(new Date());
        xkOrder1.setCourseId(xkOrder.getCourseId());
        xkOrder1.setClassId(xkOrder.getClassId());
        xkOrder1.setOrderCategoryId(xkOrder1.getOrderCategoryId());
        xkOrder1.setXkAmount(totalPrice);
        xkOrder1.setUserId(xkOrder.getUserId());
        return xkOrderMapper.insertXkOrder(xkOrder1);
    }
    /**
     * 修改消课订单
     *
     * @param xkOrder 消课订单
     * @return 结果
     */
    @Override
    public int updateXkOrder(XkOrder xkOrder)
    {
        return xkOrderMapper.updateXkOrder(xkOrder);
    }

    /**
     * 批量删除消课订单
     *
     * @param xkIds 需要删除的消课订单ID
     * @return 结果
     */
    @Override
    public int deleteXkOrderByIds(Long[] xkIds)
    {
        return xkOrderMapper.deleteXkOrderByIds(xkIds);
    }

    /**
     * 删除消课订单信息
     *
     * @param xkId 消课订单ID
     * @return 结果
     */
    @Override
    public int deleteXkOrderById(Long xkId)
    {
        return xkOrderMapper.deleteXkOrderById(xkId);
    }

    @Override
    public Page<XkOrder> getXkOrderListByPage1(Page<YsOrder> page, Integer userId,String xkDate) {
        return xkOrderMapper.getXkOrderListByPage1(page,userId,xkDate);
    }


    @Override
    public List<XkOrder> getXkOrderListByPage(XkOrder xkOrder) {
        return xkOrderMapper.getXkOrderListByPage(xkOrder);
    }

    @Override
    public List<SysUser> getTeacherList() {
        return xkOrderMapper.getTeacherList();
    }

    @Override
    public List<XkxClassStudent> getStudentHours(Integer classId) {
        return xkOrderMapper.getStudentHours(classId);
    }
}
